package cn.acewill.yxd.yunxundian.utils.baiduUtils;

import android.content.Context;
import android.text.InputFilter;
import android.text.Spanned;
import android.widget.EditText;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by XXX on 2017/11/22.
 */

public class InputUtil {

    private Context mcontext;
    private static InputUtil instance;
    private InputUtil(Context context){
        this.mcontext = context;
    }
    public static InputUtil getInstance(Context mcontext){
        if (instance==null){
            instance = new InputUtil(mcontext.getApplicationContext());
        }
        return instance;
    }

    /**
     * 禁止EditText输入特殊字符
     * @param editText
     */
    public  void setEditTextInhibitInputSpeChat(EditText editText){

        InputFilter filter=new InputFilter() {
            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
                String speChat="[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
                Pattern pattern = Pattern.compile(speChat);
                Matcher matcher = pattern.matcher(source.toString());
                if(matcher.find())return "";
                else return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter});
    }
}
